package com.wanxi.dangdang.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanxi.dangdang.mapper.RoleMapper;
import com.wanxi.dangdang.model.pojo.Role;
import com.wanxi.dangdang.model.vo.RoleFindByIdVo;
import com.wanxi.dangdang.model.vo.RolePermissionVo;
import com.wanxi.dangdang.model.vo.RoleVo;
import com.wanxi.dangdang.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 吕永杰
 * @Description:
 * @Date: Created in 2021/9/1
 * @Version: 1.0
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void add(Role role) {
        roleMapper.add(role);
    }

    @Override
    public Role findById(Long id) {
        return roleMapper.findById(id);
    }

    @Override
    public PageInfo<RoleFindByIdVo> findPage(RoleVo roleVo) {
        PageHelper.startPage(roleVo.getPageNum(),roleVo.getPageSize());
        List<RoleFindByIdVo> roles = roleMapper.findPage(roleVo);
        return new PageInfo<>(roles);
    }

    @Override
    public void updateById(Role role) {
        roleMapper.updateById(role);
    }

    @Override
    public void deleteById(Long id) {
        roleMapper.deleteAdminRoleById(id);
        roleMapper.deleteRolePermissionById(id);
        roleMapper.deleteById(id);
    }

    @Override
    public void updatePermissionsById(RolePermissionVo rolePermissionVo) {
        roleMapper.deleteRolePermissionById(rolePermissionVo.getId());
        roleMapper.updatePermissionsById(rolePermissionVo);
    }

    @Override
    public List<Role> findRolesByUserId(Long id) {
        return roleMapper.findRolesByUserId(id);
    }

    @Override
    public String checkName(String name) {
        return roleMapper.checkName(name);
    }
}
